package com.lsj.controller;

import cn.hutool.core.convert.Convert;
import com.lsj.core.response.CommonResult;
import com.lsj.core.util.ExceptionHelperUtil;
import com.lsj.core.validate.Add;
import com.lsj.core.validate.Update;
import com.lsj.dto.sys.MenuAddDto;
import com.lsj.dto.sys.MenuSortDto;
import com.lsj.dto.sys.MenuUpdateDto;
import com.lsj.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author:lin.sj
 * @Data:2021/7/12 22:40
 * @Desc:
 */
@RestController
@RequestMapping("/sys_menu")
@Api(tags = "菜单处理")
public class MenuController {
    @Autowired
    MenuService menuService;

    @PostMapping("/add")
    @ApiOperation(value = "增加菜单")
    public CommonResult addMenu(@RequestBody @Validated(Add.class) MenuAddDto dto){
        menuService.addMenu(dto);
        return  CommonResult.success();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除该菜单节点及子节点下所有信息")
    public CommonResult deleteMenu(@RequestBody Map param){
        Long menuId = Convert.toLong(param.get("menuId"));
        ExceptionHelperUtil.nullObjException(menuId,"菜单ID不能为空");
        menuService.deleteMenuAndChildren(menuId);
        return  CommonResult.success();
    }

    @PostMapping("/updateSort")
    @ApiOperation(value = "更新菜单排序")
    public CommonResult updateMenuSort(@RequestBody @Validated(Update.class)MenuSortDto menuSortDto){
        menuService.updateMenuSort(menuSortDto);
        return  CommonResult.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新菜单")
    public CommonResult updateMenu(@RequestBody @Validated(Update.class) MenuUpdateDto dto){
        menuService.updateMenu(dto);
        return  CommonResult.success();
    }

    @GetMapping("/get")
    @ApiOperation(value = "获取菜单")
    public CommonResult getMenu(@RequestParam("menuId")  Long menuId){
        ExceptionHelperUtil.nullObjException(menuId,"菜单ID不能为空");
        return CommonResult.success(menuService.getMenuById(menuId));
    }

    @GetMapping("/getPositiveMenuTree")
    @ApiOperation(value = "加载菜单节点")
    public CommonResult getPositiveMenuTree(){
        return CommonResult.success(menuService.getPositiveMenuTree());
    }

    @GetMapping("/getMenuTreeByUserId")
    @ApiOperation(value = "根据用户Id获取菜单树")
    public CommonResult getMenuTreeByUserId(@RequestParam("userId") Long userId){
        return CommonResult.success(menuService.getMenuTreeByUserId(userId));
    }

}
